<?php

namespace Shahu\Common\model;

use Shahu\Common\enum\DeleteEnum;
use support\Db;

class ApplicationModel extends BaseModel
{
    use SortModel;

    protected static string $table_name = 'admin_applications';
    protected static string $table_alias = 'app';

    /**
     * @var string 表名
     */
    protected $table = 'applications';
    /**
     * @var string[] 可操作列名数组
     */
    protected $fillable = ['app_name', 'app_logo', 'app_introduce', 'tenant', 'create_user', 'update_user'];

    protected static array $field_arr = ['id', 'app_name', 'app_logo', 'app_introduce', 'tenant', 'create_user'];

    /**
     * 根据租户查询
     * @param $tenant
     * @param string $field
     * @return array
     */
    public static function selectOneByTenant($tenant, string $field = '') : array
    {
        if (!empty($tenant)) {
            $result = Db::table(static::$table_name)
                ->select(static::getFields($field))
                ->where("tenant", $tenant)
                ->where(static::$delete_field, (DeleteEnum::LIVE)->value)
                ->limit(1)
                ->first();
            return static::handleSelectResult($result);
        }
        return [];
    }


    /**
     * 查询全部
     * @param string $field
     * @return array
     */
    public static function selectAll(string $field = ''): array
    {
        $result = Db::table(static::$table_name)
            ->select(static::getFields($field))
            ->where(static::$delete_field, (DeleteEnum::LIVE)->value)
            ->get();
        return static::handleSelectResult($result);
    }
}
